package com.simrobot.common.result;

import lombok.Data;

@Data
public class Result<T> {

    private Integer code;
    private String message;
    public T data;

    //私有化
    private Result(){}

    //返回数据
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum){
        Result<T> result = new Result<T>();
        //封装数据
        if (body != null){
            result.setData(body);
        }
        //状态码
        result.setCode(resultCodeEnum.getCode());
        //返回信息
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    //成功
    public static <T> Result<T> success(){
        return build(null,ResultCodeEnum.SUCCESS);
    }
    public static <T> Result<T> success(T data){
        return build(data,ResultCodeEnum.SUCCESS);
    }
    //失败
    public static <T> Result<T> fail(){
        return build(null,ResultCodeEnum.FAIL);
    }
    public static <T> Result<T> fail(T data){
        return build(data,ResultCodeEnum.FAIL);
    }

    public Result<T> message(String message){
        this.message = message;
        return this;
    }

    public Result<T> code(Integer code){
        this.code = code;
        return this;
    }
}
